const INIT_PEOPLE_COUNT = 1;
const CHANGE_PEOPLE_COUNT = 1;
const CLOSE = 2;

importScripts('/utils/axios.min.js');

function getAnswerCount(){
    return axios.get('/answer/count').then(({data})=>{
        if(data.code){
            return data.data;
        }
        alert(res.data.msg);
    });
}

function openInterval(num){
    let timer = null;
    clearInterval(timer);
    timer = setInterval(async function(){
        const {answerCount} = await getAnswerCount();
        if(answerCount !== num){
            self.postMessage({
                code:CHANGE_PEOPLE_COUNT,
                answerCount
            })
            num = answerCount;
        }
    },1000)
}


self.onmessage = function({data}){
    data = JSON.parse(data);
    switch(data.code){
        case INIT_PEOPLE_COUNT:
            openInterval(data.answerCount);
        break;
        case CLOSE: // 关闭本身
            self.close();
        break;
    }
}